package com.geeyuu.android.customview;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

public class CusChromeClient extends WebChromeClient {

    private final ProgressBar mProgressBar;
    private ChromeHandler mChromeHandler;
    
    public CusChromeClient(ProgressBar progressBar){
        this.mProgressBar = progressBar;
        if(mProgressBar != null){
            this.mProgressBar.setMax(100);
        }
    }
    
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);
        if(mProgressBar == null){
            return;
        }
        if(mChromeHandler == null){
            mChromeHandler = new ChromeHandler(view.getContext().getMainLooper());
        }
        mProgressBar.setProgress(newProgress);
        if(newProgress == 100){
            mChromeHandler.sendEmptyMessageDelayed(0, 500);
        }
    }
    
    private class ChromeHandler extends Handler{
        public ChromeHandler(Looper looper){
            super(looper);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mProgressBar.setProgress(0);
            mProgressBar.setVisibility(View.GONE);
        }
    }
    
}
